package com.inei.cie.capitulovi;

import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.inei.cie.Carga_Trabajo_Activity;
import com.inei.cie.R;
import com.inei.cie.capituloix.CapituloIX_Activity;
import com.inei.cie.dao.CapituloVIDao;
import com.inei.cie.entidades.P6_2;
import com.inei.cie.entidades.P6_2_4N;

public class CapituloVI_SeccionB_Evaluacion_Del_Estado_De_Conservacion_Activity
		extends Activity {

	public TableLayout tl6Bconservacion;
	public TableRow tr6B15texto, tr6B16, tr6B17, tr6B18, tr6B19;
	public TextView tvEdificacionNro;
	public Spinner sp6B151, sp6B152;
	public EditText et6B15otro;
	public CheckBox cb6B161, cb6B162, cb6B163, cb6B164, cb6B165, cb6B166;
	public EditText et6B161B, et6B162B, et6B163B, et6B164B, et6B165B;
	public EditText et6B161R, et6B162R, et6B163R, et6B164R, et6B165R;
	public EditText et6B161M, et6B162M, et6B163M, et6B164M, et6B165M,
			et6B16otro;
	public EditText et6B17buen, et6B17oxidado, et6B17deteriorado;
	public CheckBox cb6B17notiene;
	public CheckBox cb6B181, cb6B182, cb6B183, cb6B184, cb6B185, cb6B186;
	public EditText et6B181B, et6B182B, et6B183B, et6B184B, et6B185B;
	public EditText et6B181R, et6B182R, et6B183R, et6B184R, et6B185R;
	public EditText et6B181M, et6B182M, et6B183M, et6B184M, et6B185M,
			et6B18otro;
	public EditText et6B19buen, et6B19oxidado, et6B19deteriorado;

	public CapituloVIDao cap6dao;
	public String edificacion;
	public String id_local;
	public int Nro_Ed;
	public int Nro_Pred;
	public int P6_2_1;
	public int P6_2_2;
	public P6_2 obj2;
	public List<P6_2_4N> lObj2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_vi_seccion_b_evaluacion_del_estado_de_conservacion);
		IniciarComponentes();

		sp6B151.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				switch (pos) {
				case 0:
					et6B15otro.setText("");
					et6B15otro.setEnabled(false);
					tr6B15texto.setVisibility(View.GONE);
					sp6B152.setSelection(0);
					sp6B152.setEnabled(false);
					break;

				case 6:
					et6B15otro.setText("");
					sp6B152.setSelection(0);
					et6B15otro.setEnabled(false);
					tr6B15texto.setVisibility(View.GONE);
					sp6B152.setEnabled(false);
					break;

				case 7:
					et6B15otro.setEnabled(true);
					tr6B15texto.setVisibility(View.VISIBLE);
					sp6B152.setEnabled(true);
					break;

				default:
					et6B15otro.setText("");
					et6B15otro.setEnabled(false);
					tr6B15texto.setVisibility(View.GONE);
					sp6B152.setEnabled(true);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		cb6B161.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B161B.setEnabled(true);
					et6B161R.setEnabled(true);
					et6B161M.setEnabled(true);
				} else {
					et6B161B.setText("");
					et6B161R.setText("");
					et6B161M.setText("");
					et6B161B.setEnabled(false);
					et6B161R.setEnabled(false);
					et6B161M.setEnabled(false);
				}
			}
		});

		cb6B162.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B162B.setEnabled(true);
					et6B162R.setEnabled(true);
					et6B162M.setEnabled(true);
				} else {
					et6B162B.setText("");
					et6B162R.setText("");
					et6B162M.setText("");
					et6B162B.setEnabled(false);
					et6B162R.setEnabled(false);
					et6B162M.setEnabled(false);
				}
			}
		});

		cb6B163.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B163B.setEnabled(true);
					et6B163R.setEnabled(true);
					et6B163M.setEnabled(true);
				} else {
					et6B163B.setText("");
					et6B163R.setText("");
					et6B163M.setText("");
					et6B163B.setEnabled(false);
					et6B163R.setEnabled(false);
					et6B163M.setEnabled(false);
				}
			}
		});

		cb6B164.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B164B.setEnabled(true);
					et6B164R.setEnabled(true);
					et6B164M.setEnabled(true);
				} else {
					et6B164B.setText("");
					et6B164R.setText("");
					et6B164M.setText("");
					et6B164B.setEnabled(false);
					et6B164R.setEnabled(false);
					et6B164M.setEnabled(false);
				}
			}
		});

		cb6B165.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B165B.setEnabled(true);
					et6B165R.setEnabled(true);
					et6B165M.setEnabled(true);
					et6B16otro.setEnabled(true);
					et6B16otro.setVisibility(View.VISIBLE);
				} else {
					et6B165B.setText("");
					et6B165R.setText("");
					et6B165M.setText("");
					et6B16otro.setText("");
					et6B165B.setEnabled(false);
					et6B165R.setEnabled(false);
					et6B165M.setEnabled(false);
					et6B16otro.setEnabled(false);
					et6B16otro.setVisibility(View.INVISIBLE);
				}
			}
		});

		cb6B166.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B161B.setText("");
					et6B161R.setText("");
					et6B161M.setText("");
					et6B162B.setText("");
					et6B162R.setText("");
					et6B162M.setText("");
					et6B163B.setText("");
					et6B163R.setText("");
					et6B163M.setText("");
					et6B164B.setText("");
					et6B164R.setText("");
					et6B164M.setText("");
					et6B165B.setText("");
					et6B165R.setText("");
					et6B165M.setText("");
					et6B16otro.setText("");
					et6B17buen.setText("");
					et6B17oxidado.setText("");
					et6B17deteriorado.setText("");
					cb6B161.setChecked(false);
					cb6B162.setChecked(false);
					cb6B163.setChecked(false);
					cb6B164.setChecked(false);
					cb6B165.setChecked(false);
					cb6B161.setEnabled(false);
					cb6B162.setEnabled(false);
					cb6B163.setEnabled(false);
					cb6B164.setEnabled(false);
					cb6B165.setEnabled(false);
					et6B16otro.setEnabled(false);
					tl6Bconservacion.removeView(tr6B17);
				} else {
					cb6B161.setEnabled(true);
					cb6B162.setEnabled(true);
					cb6B163.setEnabled(true);
					cb6B164.setEnabled(true);
					cb6B165.setEnabled(true);
					tl6Bconservacion.removeView(tr6B17);
					tl6Bconservacion.addView(tr6B17,
							tl6Bconservacion.indexOfChild(tr6B16) + 1);
				}
			}
		});

		cb6B17notiene.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B17buen.setEnabled(true);
					et6B17oxidado.setEnabled(true);
					et6B17deteriorado.setEnabled(true);
				} else {
					et6B17buen.setEnabled(false);
					et6B17oxidado.setEnabled(false);
					et6B17deteriorado.setEnabled(false);
				}
			}
		});

		cb6B181.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B181B.setEnabled(true);
					et6B181R.setEnabled(true);
					et6B181M.setEnabled(true);
				} else {
					et6B181B.setText("");
					et6B181R.setText("");
					et6B181M.setText("");
					et6B181B.setEnabled(false);
					et6B181R.setEnabled(false);
					et6B181M.setEnabled(false);
				}
			}
		});

		cb6B182.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B182B.setEnabled(true);
					et6B182R.setEnabled(true);
					et6B182M.setEnabled(true);
				} else {
					et6B182B.setText("");
					et6B182R.setText("");
					et6B182M.setText("");
					et6B182B.setEnabled(false);
					et6B182R.setEnabled(false);
					et6B182M.setEnabled(false);
				}
			}
		});

		cb6B183.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B183B.setEnabled(true);
					et6B183R.setEnabled(true);
					et6B183M.setEnabled(true);
				} else {
					et6B183B.setText("");
					et6B183R.setText("");
					et6B183M.setText("");
					et6B183B.setEnabled(false);
					et6B183R.setEnabled(false);
					et6B183M.setEnabled(false);
				}
			}
		});

		cb6B184.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B184B.setEnabled(true);
					et6B184R.setEnabled(true);
					et6B184M.setEnabled(true);
				} else {
					et6B184B.setText("");
					et6B184R.setText("");
					et6B184M.setText("");
					et6B184B.setEnabled(false);
					et6B184R.setEnabled(false);
					et6B184M.setEnabled(false);
				}
			}
		});

		cb6B185.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B185B.setEnabled(true);
					et6B185R.setEnabled(true);
					et6B185M.setEnabled(true);
					et6B18otro.setEnabled(true);
					et6B18otro.setVisibility(View.VISIBLE);
				} else {
					et6B185B.setText("");
					et6B185R.setText("");
					et6B185M.setText("");
					et6B18otro.setText("");
					et6B185B.setEnabled(false);
					et6B185R.setEnabled(false);
					et6B185M.setEnabled(false);
					et6B18otro.setEnabled(false);
					et6B18otro.setVisibility(View.INVISIBLE);
				}
			}
		});

		cb6B186.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B181B.setText("");
					et6B181R.setText("");
					et6B181M.setText("");
					et6B182B.setText("");
					et6B182R.setText("");
					et6B182M.setText("");
					et6B183B.setText("");
					et6B183R.setText("");
					et6B183M.setText("");
					et6B184B.setText("");
					et6B184R.setText("");
					et6B184M.setText("");
					et6B185B.setText("");
					et6B185R.setText("");
					et6B185M.setText("");
					et6B18otro.setText("");
					et6B19buen.setText("");
					et6B19oxidado.setText("");
					et6B19deteriorado.setText("");
					cb6B181.setChecked(false);
					cb6B182.setChecked(false);
					cb6B183.setChecked(false);
					cb6B184.setChecked(false);
					cb6B185.setChecked(false);
					cb6B181.setEnabled(false);
					cb6B182.setEnabled(false);
					cb6B183.setEnabled(false);
					cb6B184.setEnabled(false);
					cb6B185.setEnabled(false);
					et6B18otro.setEnabled(false);
					tl6Bconservacion.removeView(tr6B19);
				} else {
					cb6B181.setEnabled(true);
					cb6B182.setEnabled(true);
					cb6B183.setEnabled(true);
					cb6B184.setEnabled(true);
					cb6B185.setEnabled(true);
					tl6Bconservacion.removeView(tr6B19);
					tl6Bconservacion.addView(tr6B19,
							tl6Bconservacion.indexOfChild(tr6B18) + 1);
				}
			}
		});

		cb6B17notiene.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton cb, boolean check) {
				// TODO Auto-generated method stub
				if (check) {
					et6B17buen.setText("");
					et6B17oxidado.setText("");
					et6B17deteriorado.setText("");
					et6B17buen.setEnabled(false);
					et6B17oxidado.setEnabled(false);
					et6B17deteriorado.setEnabled(false);
				} else {
					et6B17buen.setEnabled(true);
					et6B17oxidado.setEnabled(true);
					et6B17deteriorado.setEnabled(true);
				}
			}
		});

		super.onCreate(savedInstanceState);
	}

	private void IniciarComponentes() {
		// TODO Auto-generated method stub
		tl6Bconservacion = (TableLayout) findViewById(R.id.tl6Bconservacion);
		tr6B15texto = (TableRow) findViewById(R.id.tr6B15texto);
		tr6B16 = (TableRow) findViewById(R.id.tr6B16);
		tr6B17 = (TableRow) findViewById(R.id.tr6B17);
		tr6B18 = (TableRow) findViewById(R.id.tr6B18);
		tr6B19 = (TableRow) findViewById(R.id.tr6B19);

		tvEdificacionNro = (TextView) findViewById(R.id.tvEdificacionNro);

		sp6B151 = (Spinner) findViewById(R.id.sp6B151);
		sp6B152 = (Spinner) findViewById(R.id.sp6B152);

		et6B15otro = (EditText) findViewById(R.id.et6B15otro);

		cb6B161 = (CheckBox) findViewById(R.id.cb6B161);
		cb6B162 = (CheckBox) findViewById(R.id.cb6B162);
		cb6B163 = (CheckBox) findViewById(R.id.cb6B163);
		cb6B164 = (CheckBox) findViewById(R.id.cb6B164);
		cb6B165 = (CheckBox) findViewById(R.id.cb6B165);
		cb6B166 = (CheckBox) findViewById(R.id.cb6B166);

		et6B161B = (EditText) findViewById(R.id.et6B161B);
		et6B162B = (EditText) findViewById(R.id.et6B162B);
		et6B163B = (EditText) findViewById(R.id.et6B163B);
		et6B164B = (EditText) findViewById(R.id.et6B164B);
		et6B165B = (EditText) findViewById(R.id.et6B165B);

		et6B161R = (EditText) findViewById(R.id.et6B161R);
		et6B162R = (EditText) findViewById(R.id.et6B162R);
		et6B163R = (EditText) findViewById(R.id.et6B163R);
		et6B164R = (EditText) findViewById(R.id.et6B164R);
		et6B165R = (EditText) findViewById(R.id.et6B165R);

		et6B161M = (EditText) findViewById(R.id.et6B161M);
		et6B162M = (EditText) findViewById(R.id.et6B162M);
		et6B163M = (EditText) findViewById(R.id.et6B163M);
		et6B164M = (EditText) findViewById(R.id.et6B164M);
		et6B165M = (EditText) findViewById(R.id.et6B165M);

		et6B16otro = (EditText) findViewById(R.id.et6B16otro);

		et6B17buen = (EditText) findViewById(R.id.et6B17buen);
		et6B17oxidado = (EditText) findViewById(R.id.et6B17oxidado);
		et6B17deteriorado = (EditText) findViewById(R.id.et6B17deteriorado);

		cb6B17notiene = (CheckBox) findViewById(R.id.cb6B17notiene);

		cb6B181 = (CheckBox) findViewById(R.id.cb6B181);
		cb6B182 = (CheckBox) findViewById(R.id.cb6B182);
		cb6B183 = (CheckBox) findViewById(R.id.cb6B183);
		cb6B184 = (CheckBox) findViewById(R.id.cb6B184);
		cb6B185 = (CheckBox) findViewById(R.id.cb6B185);
		cb6B186 = (CheckBox) findViewById(R.id.cb6B186);

		et6B181B = (EditText) findViewById(R.id.et6B181B);
		et6B182B = (EditText) findViewById(R.id.et6B182B);
		et6B183B = (EditText) findViewById(R.id.et6B183B);
		et6B184B = (EditText) findViewById(R.id.et6B184B);
		et6B185B = (EditText) findViewById(R.id.et6B185B);

		et6B181R = (EditText) findViewById(R.id.et6B181R);
		et6B182R = (EditText) findViewById(R.id.et6B182R);
		et6B183R = (EditText) findViewById(R.id.et6B183R);
		et6B184R = (EditText) findViewById(R.id.et6B184R);
		et6B185R = (EditText) findViewById(R.id.et6B185R);

		et6B181M = (EditText) findViewById(R.id.et6B181M);
		et6B182M = (EditText) findViewById(R.id.et6B182M);
		et6B183M = (EditText) findViewById(R.id.et6B183M);
		et6B184M = (EditText) findViewById(R.id.et6B184M);
		et6B185M = (EditText) findViewById(R.id.et6B185M);

		et6B18otro = (EditText) findViewById(R.id.et6B18otro);

		et6B19buen = (EditText) findViewById(R.id.et6B19buen);
		et6B19oxidado = (EditText) findViewById(R.id.et6B19oxidado);
		et6B19deteriorado = (EditText) findViewById(R.id.et6B19deteriorado);

		sp6B152.setEnabled(false);
		et6B15otro.setEnabled(false);

		et6B161B.setEnabled(false);
		et6B162B.setEnabled(false);
		et6B163B.setEnabled(false);
		et6B164B.setEnabled(false);
		et6B165B.setEnabled(false);

		et6B161R.setEnabled(false);
		et6B162R.setEnabled(false);
		et6B163R.setEnabled(false);
		et6B164R.setEnabled(false);
		et6B165R.setEnabled(false);

		et6B161M.setEnabled(false);
		et6B162M.setEnabled(false);
		et6B163M.setEnabled(false);
		et6B164M.setEnabled(false);
		et6B165M.setEnabled(false);

		et6B181B.setEnabled(false);
		et6B182B.setEnabled(false);
		et6B183B.setEnabled(false);
		et6B184B.setEnabled(false);
		et6B185B.setEnabled(false);

		et6B181R.setEnabled(false);
		et6B182R.setEnabled(false);
		et6B183R.setEnabled(false);
		et6B184R.setEnabled(false);
		et6B185R.setEnabled(false);

		et6B181M.setEnabled(false);
		et6B182M.setEnabled(false);
		et6B183M.setEnabled(false);
		et6B184M.setEnabled(false);
		et6B185M.setEnabled(false);

		et6B16otro.setEnabled(false);
		et6B18otro.setEnabled(false);

		CargarDatos();
	}

	private void CargarDatos() {
		// TODO Auto-generated method stub
		id_local = getIntent().getExtras().getString("id_local");
		Nro_Ed = getIntent().getExtras().getInt("Nro_Ed");
		P6_2_1 = getIntent().getExtras().getInt("P6_2_1");
		P6_2_2 = getIntent().getExtras().getInt("P6_2_2");
		Nro_Pred = getIntent().getExtras().getInt("Nro_Pred");
		edificacion = "Local : "
				+ id_local
				+ " - Predio : "
				+ (getIntent().getExtras().getInt("TITULO_PRED") < 10 ? "0"
						: "") + getIntent().getExtras().getInt("TITULO_PRED")
				+ " - Edificaci\u00f3n Nro. " + (Nro_Ed < 10 ? "0" : "")
				+ Nro_Ed;
		tvEdificacionNro.setText(edificacion);

		cap6dao = new CapituloVIDao(getApplicationContext());
		List<Object> lista = cap6dao.obtenerAmbiente(id_local, Nro_Ed,
				Nro_Pred, P6_2_1, P6_2_2);
		obj2 = (P6_2) lista.get(0);
		lObj2 = (List<P6_2_4N>) lista.get(1);

		if (obj2.getP6_2_15() != 0) {
			sp6B151.setSelection(obj2.getP6_2_15());
			et6B15otro.setEnabled(obj2.getP6_2_15() == 7 ? true : false);
			sp6B152.setEnabled(obj2.getP6_2_15() != 0 ? true : false);
		}
		if (obj2.getP6_2_15a() != 0) {
			sp6B152.setSelection(obj2.getP6_2_15a());
		}

		et6B15otro.setText(obj2.getP6_2_15_O());

		if (obj2.getP6_2_16a() != 0) {
			cb6B161.setChecked(obj2.getP6_2_16a() == 1 ? true : false);
			et6B161B.setEnabled(obj2.getP6_2_16a() == 1 ? true : false);
			et6B161R.setEnabled(obj2.getP6_2_16a() == 1 ? true : false);
			et6B161M.setEnabled(obj2.getP6_2_16a() == 1 ? true : false);
		}
		if (obj2.getP6_2_16b() != 0) {
			cb6B162.setChecked(obj2.getP6_2_16b() == 1 ? true : false);
			et6B162B.setEnabled(obj2.getP6_2_16b() == 1 ? true : false);
			et6B162R.setEnabled(obj2.getP6_2_16b() == 1 ? true : false);
			et6B162M.setEnabled(obj2.getP6_2_16b() == 1 ? true : false);
		}
		if (obj2.getP6_2_16c() != 0) {
			cb6B163.setChecked(obj2.getP6_2_16c() == 1 ? true : false);
			et6B163B.setEnabled(obj2.getP6_2_16c() == 1 ? true : false);
			et6B163R.setEnabled(obj2.getP6_2_16c() == 1 ? true : false);
			et6B163M.setEnabled(obj2.getP6_2_16c() == 1 ? true : false);
		}
		if (obj2.getP6_2_16d() != 0) {
			cb6B164.setChecked(obj2.getP6_2_16d() == 1 ? true : false);
			et6B164B.setEnabled(obj2.getP6_2_16d() == 1 ? true : false);
			et6B164R.setEnabled(obj2.getP6_2_16d() == 1 ? true : false);
			et6B164M.setEnabled(obj2.getP6_2_16d() == 1 ? true : false);
		}
		if (obj2.getP6_2_16e() != 0) {
			cb6B165.setChecked(obj2.getP6_2_16e() == 1 ? true : false);
			et6B165B.setEnabled(obj2.getP6_2_16e() == 1 ? true : false);
			et6B165R.setEnabled(obj2.getP6_2_16e() == 1 ? true : false);
			et6B165M.setEnabled(obj2.getP6_2_16e() == 1 ? true : false);
			et6B16otro.setEnabled(obj2.getP6_2_16e() == 1 ? true : false);
		}
		if (obj2.getP6_2_16f() != 0) {
			cb6B166.setChecked(obj2.getP6_2_16f() == 1 ? true : false);
			cb6B161.setEnabled(false);
			cb6B162.setEnabled(false);
			cb6B163.setEnabled(false);
			cb6B164.setEnabled(false);
			cb6B165.setEnabled(false);
			if (obj2.getP6_2_16f() == 1) {
				tl6Bconservacion.removeView(tr6B17);
			}
		}

		if (obj2.getP6_2_16a_b() != 0) {
			et6B161B.setText(obj2.getP6_2_16a_b() + "");
		}
		if (obj2.getP6_2_16b_b() != 0) {
			et6B162B.setText(obj2.getP6_2_16b_b() + "");
		}
		if (obj2.getP6_2_16c_b() != 0) {
			et6B163B.setText(obj2.getP6_2_16c_b() + "");
		}
		if (obj2.getP6_2_16d_b() != 0) {
			et6B164B.setText(obj2.getP6_2_16d_b() + "");
		}
		if (obj2.getP6_2_16e_b() != 0) {
			et6B165B.setText(obj2.getP6_2_16e_b() + "");
		}
		if (obj2.getP6_2_16a_r() != 0) {
			et6B161R.setText(obj2.getP6_2_16a_r() + "");
		}
		if (obj2.getP6_2_16b_r() != 0) {
			et6B162R.setText(obj2.getP6_2_16b_r() + "");
		}
		if (obj2.getP6_2_16c_r() != 0) {
			et6B163R.setText(obj2.getP6_2_16c_r() + "");
		}
		if (obj2.getP6_2_16d_r() != 0) {
			et6B164R.setText(obj2.getP6_2_16d_r() + "");
		}
		if (obj2.getP6_2_16e_r() != 0) {
			et6B165R.setText(obj2.getP6_2_16e_r() + "");
		}
		if (obj2.getP6_2_16a_m() != 0) {
			et6B161M.setText(obj2.getP6_2_16a_m() + "");
		}
		if (obj2.getP6_2_16b_m() != 0) {
			et6B162M.setText(obj2.getP6_2_16b_m() + "");
		}
		if (obj2.getP6_2_16c_m() != 0) {
			et6B163M.setText(obj2.getP6_2_16c_m() + "");
		}
		if (obj2.getP6_2_16d_m() != 0) {
			et6B164M.setText(obj2.getP6_2_16d_m() + "");
		}
		if (obj2.getP6_2_16e_m() != 0) {
			et6B165M.setText(obj2.getP6_2_16e_m() + "");
		}

		et6B16otro.setText(obj2.getP6_2_16e_O());

		if (obj2.getP6_2_17a() != 0) {
			et6B17buen.setText(obj2.getP6_2_17a() + "");
		}
		if (obj2.getP6_2_17b() != 0) {
			et6B17oxidado.setText(obj2.getP6_2_17b() + "");
		}
		if (obj2.getP6_2_17c() != 0) {
			et6B17deteriorado.setText(obj2.getP6_2_17c() + "");
		}
		if (obj2.getP6_2_17d() != 0) {
			cb6B17notiene.setChecked(obj2.getP6_2_17d() == 1 ? true : false);
			et6B17buen.setEnabled(false);
			et6B17oxidado.setEnabled(false);
			et6B17deteriorado.setEnabled(false);
		}
		if (obj2.getP6_2_18a() != 0) {
			cb6B181.setChecked(obj2.getP6_2_18a() == 1 ? true : false);
			et6B181B.setEnabled(obj2.getP6_2_18a() == 1 ? true : false);
			et6B181R.setEnabled(obj2.getP6_2_18a() == 1 ? true : false);
			et6B181M.setEnabled(obj2.getP6_2_18a() == 1 ? true : false);
		}
		if (obj2.getP6_2_18b() != 0) {
			cb6B182.setChecked(obj2.getP6_2_18b() == 1 ? true : false);
			et6B182B.setEnabled(obj2.getP6_2_18b() == 1 ? true : false);
			et6B182R.setEnabled(obj2.getP6_2_18b() == 1 ? true : false);
			et6B182M.setEnabled(obj2.getP6_2_18b() == 1 ? true : false);
		}
		if (obj2.getP6_2_18c() != 0) {
			cb6B183.setChecked(obj2.getP6_2_18c() == 1 ? true : false);
			et6B183B.setEnabled(obj2.getP6_2_18c() == 1 ? true : false);
			et6B183R.setEnabled(obj2.getP6_2_18c() == 1 ? true : false);
			et6B183M.setEnabled(obj2.getP6_2_18c() == 1 ? true : false);
		}
		if (obj2.getP6_2_18d() != 0) {
			cb6B184.setChecked(obj2.getP6_2_18d() == 1 ? true : false);
			et6B184B.setEnabled(obj2.getP6_2_18d() == 1 ? true : false);
			et6B184R.setEnabled(obj2.getP6_2_18d() == 1 ? true : false);
			et6B184M.setEnabled(obj2.getP6_2_18d() == 1 ? true : false);
		}
		if (obj2.getP6_2_18e() != 0) {
			cb6B185.setChecked(obj2.getP6_2_18e() == 1 ? true : false);
			et6B185B.setEnabled(obj2.getP6_2_18e() == 1 ? true : false);
			et6B185R.setEnabled(obj2.getP6_2_18e() == 1 ? true : false);
			et6B185M.setEnabled(obj2.getP6_2_18e() == 1 ? true : false);
			et6B18otro.setEnabled(obj2.getP6_2_18e() == 1 ? true : false);
		}
		if (obj2.getP6_2_18f() != 0) {
			cb6B186.setChecked(obj2.getP6_2_18f() == 1 ? true : false);
			cb6B181.setEnabled(false);
			cb6B182.setEnabled(false);
			cb6B183.setEnabled(false);
			cb6B184.setEnabled(false);
			cb6B185.setEnabled(false);
			if (obj2.getP6_2_18f() == 1) {
				tl6Bconservacion.removeView(tr6B19);
			}
		}
		if (obj2.getP6_2_18a_b() != 0) {
			et6B181B.setText(obj2.getP6_2_18a_b() + "");
		}
		if (obj2.getP6_2_18b_b() != 0) {
			et6B182B.setText(obj2.getP6_2_18b_b() + "");
		}
		if (obj2.getP6_2_18c_b() != 0) {
			et6B183B.setText(obj2.getP6_2_18c_b() + "");
		}
		if (obj2.getP6_2_18d_b() != 0) {
			et6B184B.setText(obj2.getP6_2_18d_b() + "");
		}
		if (obj2.getP6_2_18e_b() != 0) {
			et6B185B.setText(obj2.getP6_2_18e_b() + "");
		}
		if (obj2.getP6_2_18a_r() != 0) {
			et6B181R.setText(obj2.getP6_2_18a_r() + "");
		}
		if (obj2.getP6_2_18b_r() != 0) {
			et6B182R.setText(obj2.getP6_2_18b_r() + "");
		}
		if (obj2.getP6_2_18c_r() != 0) {
			et6B183R.setText(obj2.getP6_2_18c_r() + "");
		}
		if (obj2.getP6_2_18d_r() != 0) {
			et6B184R.setText(obj2.getP6_2_18d_r() + "");
		}
		if (obj2.getP6_2_18e_r() != 0) {
			et6B185R.setText(obj2.getP6_2_18e_r() + "");
		}
		if (obj2.getP6_2_18a_m() != 0) {
			et6B181M.setText(obj2.getP6_2_18a_m() + "");
		}
		if (obj2.getP6_2_18b_m() != 0) {
			et6B182M.setText(obj2.getP6_2_18b_m() + "");
		}
		if (obj2.getP6_2_18c_m() != 0) {
			et6B183M.setText(obj2.getP6_2_18c_m() + "");
		}
		if (obj2.getP6_2_18d_m() != 0) {
			et6B184M.setText(obj2.getP6_2_18d_m() + "");
		}
		if (obj2.getP6_2_18e_m() != 0) {
			et6B185M.setText(obj2.getP6_2_18e_m() + "");
		}

		et6B18otro.setText(obj2.getP6_2_18e_O());

		if (obj2.getP6_2_19a() != 0) {
			et6B19buen.setText(obj2.getP6_2_19a() + "");
		}
		if (obj2.getP6_2_19b() != 0) {
			et6B19oxidado.setText(obj2.getP6_2_19b() + "");
		}
		if (obj2.getP6_2_19c() != 0) {
			et6B19deteriorado.setText(obj2.getP6_2_19c() + "");
		}
	}

	public void guardar() {
		obj2.setP6_2_15(sp6B151.getSelectedItemPosition());
		obj2.setP6_2_15a(sp6B152.getSelectedItemPosition());
		obj2.setP6_2_15_O(et6B15otro.getText().toString().trim().length() == 0 ? ""
				: et6B15otro.getText().toString());

		obj2.setP6_2_16a(cb6B161.isChecked() ? 1 : 0);
		obj2.setP6_2_16b(cb6B162.isChecked() ? 1 : 0);
		obj2.setP6_2_16c(cb6B163.isChecked() ? 1 : 0);
		obj2.setP6_2_16d(cb6B164.isChecked() ? 1 : 0);
		obj2.setP6_2_16e(cb6B165.isChecked() ? 1 : 0);
		obj2.setP6_2_16f(cb6B166.isChecked() ? 1 : 0);

		obj2.setP6_2_16a_b(et6B161B.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B161B.getText().toString()));
		obj2.setP6_2_16b_b(et6B162B.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B162B.getText().toString()));
		obj2.setP6_2_16c_b(et6B163B.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B163B.getText().toString()));
		obj2.setP6_2_16d_b(et6B164B.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B164B.getText().toString()));
		obj2.setP6_2_16e_b(et6B165B.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B165B.getText().toString()));

		obj2.setP6_2_16a_r(et6B161R.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B161R.getText().toString()));
		obj2.setP6_2_16b_r(et6B162R.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B162R.getText().toString()));
		obj2.setP6_2_16c_r(et6B163R.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B163R.getText().toString()));
		obj2.setP6_2_16d_r(et6B164R.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B164R.getText().toString()));
		obj2.setP6_2_16e_r(et6B165R.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B165R.getText().toString()));

		obj2.setP6_2_16a_m(et6B161M.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B161M.getText().toString()));
		obj2.setP6_2_16b_m(et6B162M.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B162M.getText().toString()));
		obj2.setP6_2_16c_m(et6B163M.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B163M.getText().toString()));
		obj2.setP6_2_16d_m(et6B164M.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B164M.getText().toString()));
		obj2.setP6_2_16e_m(et6B165M.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B165M.getText().toString()));

		obj2.setP6_2_16e_O(et6B16otro.getText().toString());

		obj2.setP6_2_17a(et6B17buen.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B17buen.getText().toString()));
		obj2.setP6_2_17b(et6B17oxidado.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B17oxidado.getText().toString()));
		obj2.setP6_2_17c(et6B17deteriorado.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B17deteriorado.getText().toString()));
		obj2.setP6_2_17d(cb6B17notiene.isChecked() ? 1 : 0);

		obj2.setP6_2_18a(cb6B181.isChecked() ? 1 : 0);
		obj2.setP6_2_18b(cb6B182.isChecked() ? 1 : 0);
		obj2.setP6_2_18c(cb6B183.isChecked() ? 1 : 0);
		obj2.setP6_2_18d(cb6B184.isChecked() ? 1 : 0);
		obj2.setP6_2_18e(cb6B185.isChecked() ? 1 : 0);
		obj2.setP6_2_18f(cb6B186.isChecked() ? 1 : 0);

		obj2.setP6_2_18a_b(et6B181B.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B181B.getText().toString()));
		obj2.setP6_2_18b_b(et6B182B.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B182B.getText().toString()));
		obj2.setP6_2_18c_b(et6B183B.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B183B.getText().toString()));
		obj2.setP6_2_18d_b(et6B184B.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B184B.getText().toString()));
		obj2.setP6_2_18e_b(et6B185B.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B185B.getText().toString()));

		obj2.setP6_2_18a_r(et6B181R.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B181R.getText().toString()));
		obj2.setP6_2_18b_r(et6B182R.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B182R.getText().toString()));
		obj2.setP6_2_18c_r(et6B183R.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B183R.getText().toString()));
		obj2.setP6_2_18d_r(et6B184R.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B184R.getText().toString()));
		obj2.setP6_2_18e_r(et6B185R.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B185R.getText().toString()));

		obj2.setP6_2_18a_m(et6B181M.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B181M.getText().toString()));
		obj2.setP6_2_18b_m(et6B182M.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B182M.getText().toString()));
		obj2.setP6_2_18c_m(et6B183M.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B183M.getText().toString()));
		obj2.setP6_2_18d_m(et6B184M.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B184M.getText().toString()));
		obj2.setP6_2_18e_m(et6B185M.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B185M.getText().toString()));

		obj2.setP6_2_18e_O(et6B18otro.getText().toString());

		obj2.setP6_2_19a(et6B19buen.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B19buen.getText().toString()));
		obj2.setP6_2_19b(et6B19oxidado.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B19oxidado.getText().toString()));
		obj2.setP6_2_19c(et6B19deteriorado.getText().toString().trim().length() == 0 ? 0
				: Integer.parseInt(et6B19deteriorado.getText().toString()));

		for (P6_2_4N p6_2_4n : lObj2) {
			p6_2_4n.setSwCompleto(true);
		}

		obj2.setSwCompleto(true);

		cap6dao.actualizarSeccionB(obj2, lObj2);

		Intent irMenu = new Intent(this,
				CapituloVI_SeccionB_Lista_Ambientes_Activity.class);
		irMenu.putExtras(getIntent().getExtras());
		startActivity(irMenu);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_capitulovi, menu);
		menu.getItem(0).setTitle("AMBIENTES POR PISO");
		menu.getItem(1).setTitle("TERMINAR");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir ?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnMarco:
			Intent irMarco = new Intent(this, Carga_Trabajo_Activity.class);
			irMarco.putExtras(getIntent().getExtras());
			startActivity(irMarco);
			finish();
			break;
		case R.id.mnIncio:
			Intent marco = new Intent(this,
					CapituloVI_SeccionB_Lista_Ambientes_Activity.class);
			marco.putExtras(getIntent().getExtras());
			startActivity(marco);
			finish();
			break;
		case R.id.mnSiguiente:
			int ventanas = Integer.parseInt((et6B181B.getText().toString()
					.length() == 0 ? "0" : et6B181B.getText().toString()));
			ventanas += Integer.parseInt((et6B181R.getText().toString()
					.length() == 0 ? "0" : et6B181R.getText().toString()));
			ventanas += Integer.parseInt((et6B181M.getText().toString()
					.length() == 0 ? "0" : et6B181M.getText().toString()));
			ventanas += Integer.parseInt((et6B182B.getText().toString()
					.length() == 0 ? "0" : et6B182B.getText().toString()));
			ventanas += Integer.parseInt((et6B182R.getText().toString()
					.length() == 0 ? "0" : et6B182R.getText().toString()));
			ventanas += Integer.parseInt((et6B182M.getText().toString()
					.length() == 0 ? "0" : et6B182M.getText().toString()));
			ventanas += Integer.parseInt((et6B183B.getText().toString()
					.length() == 0 ? "0" : et6B183B.getText().toString()));
			ventanas += Integer.parseInt((et6B183R.getText().toString()
					.length() == 0 ? "0" : et6B183R.getText().toString()));
			ventanas += Integer.parseInt((et6B183M.getText().toString()
					.length() == 0 ? "0" : et6B183M.getText().toString()));
			ventanas += Integer.parseInt((et6B184B.getText().toString()
					.length() == 0 ? "0" : et6B184B.getText().toString()));
			ventanas += Integer.parseInt((et6B184R.getText().toString()
					.length() == 0 ? "0" : et6B184R.getText().toString()));
			ventanas += Integer.parseInt((et6B184M.getText().toString()
					.length() == 0 ? "0" : et6B184M.getText().toString()));
			ventanas += Integer.parseInt((et6B185B.getText().toString()
					.length() == 0 ? "0" : et6B185B.getText().toString()));
			ventanas += Integer.parseInt((et6B185R.getText().toString()
					.length() == 0 ? "0" : et6B185R.getText().toString()));
			ventanas += Integer.parseInt((et6B185M.getText().toString()
					.length() == 0 ? "0" : et6B185M.getText().toString()));

			int vidrios = Integer.parseInt((et6B19buen.getText().toString()
					.length() == 0 ? "0" : et6B19buen.getText().toString()));
			vidrios += Integer.parseInt((et6B19oxidado.getText().toString()
					.length() == 0 ? "0" : et6B19oxidado.getText().toString()));
			vidrios += Integer.parseInt((et6B19deteriorado.getText().toString()
					.length() == 0 ? "0" : et6B19deteriorado.getText()
					.toString()));

			if (sp6B151.getSelectedItemPosition() != 6) {
				if (sp6B151.getSelectedItemPosition() == 0) {
					Toast.makeText(
							CapituloVI_SeccionB_Evaluacion_Del_Estado_De_Conservacion_Activity.this,
							"Debe seleccionar material predominante",
							Toast.LENGTH_SHORT).show();
					break;
				} else {
					if (sp6B151.getSelectedItemPosition() == 7
							&& et6B15otro.getText().toString().length() == 0) {
						Toast.makeText(
								CapituloVI_SeccionB_Evaluacion_Del_Estado_De_Conservacion_Activity.this,
								"Debe especificar el otro tipo de material predominante",
								Toast.LENGTH_SHORT).show();
						break;
					}
					if (sp6B152.getSelectedItemPosition() == 0) {
						Toast.makeText(
								CapituloVI_SeccionB_Evaluacion_Del_Estado_De_Conservacion_Activity.this,
								"Debe seleccionar el estado del material predominante",
								Toast.LENGTH_SHORT).show();
						break;
					}
				}
			}

			if (ventanas != vidrios) {
				Toast.makeText(
						CapituloVI_SeccionB_Evaluacion_Del_Estado_De_Conservacion_Activity.this,
						"La cantidad de ventanas debe ser igual a la cantidad de vidrios",
						Toast.LENGTH_SHORT).show();
				break;
			}

			guardar();
			break;
		case R.id.mnFoto:
			Intent foto = new Intent(CapituloVI_SeccionB_Evaluacion_Del_Estado_De_Conservacion_Activity.this,
					CapituloIX_Activity.class);
			// b.putBoolean("pregunta", true);
			foto.putExtras(getIntent().getExtras());
			foto.putExtra("pregunta", true);
			startActivity(foto);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent irPredio = new Intent(this,
				CapituloVI_SeccionB_Lista_Ambientes_Activity.class);
		irPredio.putExtras(getIntent().getExtras());
		startActivity(irPredio);
		finish();
	}
}
